package hot;

public class siba {
//    https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=top-100-liked
        public static void rotate(int[][] matrix) {
            int n = matrix.length;

            for (int layer = 0; layer < n / 2; layer++) {
                int start = layer;
                int end = n - 1 - layer;

                for (int i = start; i < end; i++) {
                    int offset = i - start;

                    // 保存左上角
                    int temp = matrix[start][i];

                    // 左下 → 左上
                    matrix[start][i] = matrix[end - offset][start];

                    // 右下 → 左下
                    matrix[end - offset][start] = matrix[end][end - offset];

                    // 右上 → 右下
                    matrix[end][end - offset] = matrix[i][end];

                    // 保存的值 → 右上
                    matrix[i][end] = temp;
                }
            }
        }

    public static void main(String[] args) {
        int[][] nums={{1,2,3},{4,5,6},{7,8,9}};
        rotate(nums);
        for(int i=0;i<3;i++){
            System.out.println();
            for(int j=0;j<3;j++){
                System.out.print(nums[i][j]+" ");
            }
        }
    }
    }

